package test;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * 部门相关遍历题，递归题
 */
public class 部门遍历树 {

    /**
     * 借助二叉树的层次遍历
     */
    public List<Long> roundSearch(Department root,String searchName) {
        List<Long> result = new ArrayList<>();
        if(root == null || root.children == null || root.children.size() <= 0) {
            return result;
        }

        Queue<Department> queue = new ArrayDeque<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            // 控制层数
            for (int i = 0; i < queue.size(); i++) {
                Department poll = queue.poll();
                if(searchName.equals(poll.name)) {
                    result.add(poll.id);
                }

                if(!poll.children.isEmpty()) {
                    for (int j = 0; j < poll.children.size(); j++) {
                        queue.offer(poll.children.get(j));
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {

    }

    class Department{
        long id;
        String name;
        List<Department> children;
    }
}
